package com.yicong.ants.ui.order;

import android.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;

import com.cchao.simplelib.core.JsonHelper;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.base.RespBean;
import com.yicong.ants.bean.order.ScenicOrder;
import com.yicong.ants.bean.scenic.TicketResultBean;
import com.yicong.ants.databinding.DialogOrderQrBinding;
import com.yicong.ants.databinding.OrderDetailActivityBinding;
import com.yicong.ants.manager.ImageHelper;
import com.yicong.ants.manager.RouterHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.AmapHelper;
import com.yicong.ants.utils.AppUtils;
import com.yicong.ants.view.ToCheckTicketsPop;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

/**
 * @author cchao
 * @version 2019-08-23.
 */
public class ScenicOrderDetailActivity extends BaseTitleBarActivity<OrderDetailActivityBinding> implements View.OnClickListener {
    ScenicOrder mBean;
    String mCategory;

    @Override
    protected int getLayout() {
        return R.layout.order_detail_activity;
    }

    @Override
    protected void initEventAndData() {
        mBean = JsonHelper.toObject(getIntent().getStringExtra(Constants.Extra.JSON), ScenicOrder.class);
        mCategory = getIntent().getStringExtra(Constants.Extra.Category);
        setTitleText("门票详情");
        mDataBind.setBean(mBean);
        mDataBind.setUser(UserManager.getUserBean());
        mDataBind.setClick(this);
        UiHelper.setVisibleElseGone(mDataBind.advanceTip, isPortal());
        addTitleMenuItem(R.drawable.scenic_detail_tel, v -> {
//            String tel = UiHelper.getString(R.string.app_tel);
//            if (StringHelper.isNotEmpty(mBean.getTelephone())) {
//                tel = mBean.getTelephone();
//            }

            AppUtils.jumpToTel(ScenicOrderDetailActivity.this, UiHelper.getString(R.string.app_tel));
        });
    }

    @Override
    protected void onLoadData() {

    }

    boolean isPortal() {
        return Constants.Extra.Portal.equals(mCategory);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image:
            case R.id.scenic_name:
                RouterHelper.toScenicDetail(thisActivity, mBean.getScenic_id());
                break;
            case R.id.address_field:
                PopupWindow mMapPopup = AmapHelper.showNavigatePop(mContext, mBean.getAddress(), mBean.getLatitude(), mBean.getLongitude());
                mMapPopup.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
                break;
            case R.id.cancel:
                break;
            case R.id.visitor_info:
                if (mBean.getTicket_status() != 10) {
                    showToast("该门票还未出票，请稍后再试");
                }
                showQrDialog();
                break;
        }
    }

    /**
     * 显示游客的二维码
     */
    private void showQrDialog() {
        // 获取对应 二维码
        String ticketCodeUrl = "";
        showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("id", mBean.getId());
        Observable<RespBean<TicketResultBean>> observable;
        if (isPortal()) {
            observable = RetrofitHelper.getApis().getScenicTicket(map);
        } else {
            observable = RetrofitHelper.getApis().getAddedScenicTicket(map);
        }

        addSubscribe(observable
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                showTicketInfo(respBean.getData());
            }, RxHelper.getHideProgressConsumer(this)));
    }

    private void showTicketInfo(TicketResultBean data) {
        switch (data.getTicket_type()) {
            case "sms":
                UiHelper.showConfirmDialog(mContext, data.getTicket_content(), null);
                break;
            case "qr_code":
            case "img":
                // show 二维码
                DialogOrderQrBinding binding = DataBindingUtil.inflate(mLayoutInflater, R.layout.dialog_order_qr, null, false);
                binding.name.setText("游客：" + UserManager.getUserBean().getId_card_name());
                ImageHelper.loadImage(binding.qrCode, data.getTicket_content());
                AlertDialog dialog = new AlertDialog.Builder(mContext)
                    .setView(binding.getRoot())
                    .setCancelable(true)
                    .setPositiveButton("确定", (dialog2, which) -> dialog2.dismiss()).create();
                binding.close.setOnClickListener(view -> dialog.dismiss());
                dialog.show();

                dialog.getWindow().setLayout((int) (UiHelper.getScreenWidth() * 4.5f / 5), WindowManager.LayoutParams.WRAP_CONTENT);
                break;
            case "html":
                if (null == ticketsPop) {
                    ticketsPop = new ToCheckTicketsPop(mContext);
                }
                ticketsPop.setHtmlContent(data.getTicket_content(), false);
                ticketsPop.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
                break;
        }
    }

    /**
     * 显示我的票务信息
     *
     * @param htmlContent
     * @param isImg
     */
    private ToCheckTicketsPop ticketsPop;

    private void showTickets(String htmlContent, TicketResultBean bean) {
        if (null == ticketsPop) {
            ticketsPop = new ToCheckTicketsPop(mContext);
        }
        ticketsPop.setHtmlContent(htmlContent, bean != null && bean.getTicket_type().equals(Constants.IMG));
        ticketsPop.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    boolean checkInputFail(EditText editText, String toast) {
        if (StringHelper.isEmpty(editText)) {
            showToast(toast);
            return true;
        }
        return false;
    }
}
